package org.jadestudio.dsdown.util;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.ibatis.sqlmap.client.SqlMapClient;

@Component
public class SysInfo  {
	
    private static Logger logger = Logger.getLogger(SysInfo.class);
	static Map<String,Map> configMap ;
	@Resource
	private SqlMapClient sqlMapClient;
	
	/*************系统变量*************/
	public static int manageRecordsPerPage; 
	public static int maxPicScaredWidth;
	public static int maxPicScaredHeight;
	public static String remotePicSavedHost;
	public static String uploadFolderName;
	public static String keyWordSplit;
	public static String domain;
	public static String downloadSwitch;
	
	/*************版本*************/
	public static String currentVersion;
	public static String currentCompileDate;
	
	/*************淘宝客*************/
	public static String appKey;
	public static String appSecret;
	public static String appUrl;
	public static String appPid;
	public static int tNum=0;
	
	@PostConstruct
	public void reload(){
		try{
			//ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		    //SqlMapClient sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
			configMap = new HashMap<String,Map>();
			List<Map> lt=(List<Map>)sqlMapClient.queryForList("test.serverCfgQuery");
			logger.info("读取服务器参数"+tNum);
			tNum++;
			for(int t=0;t<lt.size();t++){
				configMap.put((String)lt.get(t).get("key"), lt.get(t));
				logger.info("loading -> "+lt.get(t).get("key"));
			}
			logger.info("读取服务器参数完毕");
			manageRecordsPerPage = Integer.valueOf((String)configMap.get("manageRecordsPerPage").get("value")); //后台管理页，1页显示多少条记录 
			maxPicScaredWidth=Integer.valueOf((String)configMap.get("maxPicScaredWidth").get("value"));//文章图片缩略图生成宽度
			maxPicScaredHeight=Integer.valueOf((String)configMap.get("maxPicScaredHeight").get("value"));//文章图片缩略图生成高度
			remotePicSavedHost=(String)configMap.get("remotePicSavedHost").get("value");//远程图片本地化时的归属域名检测
			uploadFolderName=(String)configMap.get("uploadFolderName").get("value");//后台游戏封面上传及附件图片上传时的目录名(还需要自行在webroot下创建)
			keyWordSplit=(String)configMap.get("keyWordSplit").get("value");//文章关键字抓取时的分隔符
			domain=(String)configMap.get("domain").get("value");//当前域名
			downloadSwitch=(String)configMap.get("downloadSwitch").get("value");//下载开关
			/*************版本*************/
			currentVersion = (String)configMap.get("currentVersion").get("value");//后台版本号
			currentCompileDate =(String)configMap.get("currentCompileDate").get("value");//编译时间
			/*************淘宝客*************/
			appKey = (String)configMap.get("appKey").get("value");//appkey
			appSecret = (String)configMap.get("appSecret").get("value");//appSecret
			appUrl = (String)configMap.get("appUrl").get("value");//服务商URL
			appPid = (String)configMap.get("appPid").get("value");//个人PID
			logger.info("服务器参数加载完毕");
		}catch(Exception e){
			e.printStackTrace();
			if(tNum==0){
				logger.info("参数加载失败！系统终止");
				System.exit(0);
			}
		}
	}
	
	static Map<String,Long> memUsed;
	/**
	 *  得到系统信息
	 *  java.runtime.name 
	 *	os.arch 
	 *	java.version
	 *	sun.desktop
	 *	os.name
	 *	sun.cpu.isalist
	 *	java.library.path
	 *  @return
	 */
	public static Properties getSysInfo(){
		   Properties ps= System.getProperties();
		   return ps;
	}
	
	/**
	 * 
	 * 得到内存使用情况
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map getMemUsedState(){
		  memUsed = new HashMap();
		  MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
	      MemoryUsage heap = mem.getHeapMemoryUsage();
	      memUsed.put("mem_use", heap.getUsed()/1048576);
	      memUsed.put("mem_max", heap.getMax()/1048576);
	      return memUsed;
	}
	
	



	
	
	
}
